package com.lineage.data.item_etcitem.hp;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1BuffUtil;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UserAddHp_1 extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(UserAddHp_1.class);
  
  private int _min_hp;
  
  private int _gfxid;
  
  private int _consumeItemId;
  
  private int _consumeItemCount;
  
  public static ItemExecutor get() {
    return new UserAddHp_1();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (item == null)
      return; 
    if (pc == null)
      return; 
    if (L1BuffUtil.stopPotion(pc))
      if (pc.getInventory().consumeItem(this._consumeItemId, this._consumeItemCount)) {
        UseHeallingPotion(pc, this._min_hp, this._gfxid);
      } else {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("所需道具不足"));
      }  
  }
  
  private static void UseHeallingPotion(L1PcInstance pc, int healHp, int gfxid) {
    L1BuffUtil.cancelAbsoluteBarrier(pc);
    Random random = new Random();
    pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pc.getId(), gfxid));
    healHp = (int)(healHp * (random.nextGaussian() / 5.0D + 1.0D));
    if (pc.get_up_hp_potion() > 0) {
      healHp += healHp * pc.get_up_hp_potion() / 100;
      healHp += pc.get_uhp_number();
    } 
    if (pc.get_other().get_hppotion() > 0)
      healHp += healHp * pc.get_other().get_hppotion() / 100; 
    if (pc.get_other2().get_hppotion() > 0)
      healHp += healHp * pc.get_other2().get_hppotion() / 100; 
    if (pc.get_potion_heal() > 0)
      healHp += healHp * pc.get_potion_heal() / 100; 
    if (pc.getattr_potion_heal() > 0)
      healHp += pc.getattr_potion_heal(); 
    if (pc.hasSkillEffect(173))
      healHp >>= 1; 
    if (pc.hasSkillEffect(230))
      healHp >>= 1; 
    if (pc.hasSkillEffect(4012))
      healHp >>= 1; 
    if (pc.hasSkillEffect(4011))
      healHp *= -1; 
    if (pc.isGm())
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("GM显示当下喝水量:" + healHp)); 
    if (pc.getCurrentHp() != pc.getMaxHp())
      pc.setCurrentHp(pc.getCurrentHp() + healHp); 
  }
  
  public void set_set(String[] set) {
    try {
      this._min_hp = Integer.parseInt(set[1]);
      if (this._min_hp <= 0) {
        this._min_hp = 1;
        _log.error("UserHpr 设置错误:最小恢复值小于等于0! 使用预设1");
      } 
    } catch (Exception exception) {}
    try {
      this._gfxid = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
    try {
      this._consumeItemId = Integer.parseInt(set[3]);
      this._consumeItemCount = Integer.parseInt(set[4]);
    } catch (Exception exception) {}
  }
}
